package com.ambercrm.business;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ambercrm.R;
import com.ambercrm.base.BaseTitleActivity;
import com.ambercrm.constant.UrlConstant;
import com.ambercrm.custom_view.ClearEditText;
import com.ambercrm.custom_view.TextInputLayout;
import com.ambercrm.tools.EmptyUtils;
import com.ambercrm.tools.LogUtils;

import org.json.JSONObject;

import java.util.HashMap;

public class AccountCheckActivity extends BaseTitleActivity implements View.OnClickListener {
    private TextInputLayout phoneNumberTextInputLayout;
    private ClearEditText etPhone;
    private TextInputLayout codeTextInputLayout;
    private ClearEditText etCode;
    private TextView tvGetCode;
    private Button btnNext;
    private CountDownTimer mCountDownTimer;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_account_check;
    }

    @Override
    protected void initContentView(View v) {
        hideLine();
        phoneNumberTextInputLayout = findViewById(R.id.phone_number_text_input_layout);
        etPhone = findViewById(R.id.et_phone);
        codeTextInputLayout = findViewById(R.id.code_text_input_layout);
        etCode = findViewById(R.id.et_code);
        tvGetCode = findViewById(R.id.tv_get_code);
        btnNext = findViewById(R.id.btn_next);
        tvGetCode.setOnClickListener(this);
        btnNext.setOnClickListener(this);
        mCountDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvGetCode.setClickable(false); //设置不可点击
                tvGetCode.setTextColor(getResources().getColor(R.color.common_color_d2d4dd));
                tvGetCode.setText(String.format(getResources().getString(R.string.get_code_time), millisUntilFinished / 1000 + "s"));  //设置倒计时时间
            }

            @Override
            public void onFinish() {
                tvGetCode.setClickable(true);
                tvGetCode.setTextColor(getResources().getColor(R.color.common_color_42CC8B));
                tvGetCode.setText(R.string.send_identify_code);
            }
        };

        etPhone.setOnTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onTextChange(String s) {
                String trim = s.trim();
                LogUtils.showLog("trim=" + trim);
                if (EmptyUtils.notEmpty(trim)) {
                    if (trim.length() == 11) {
                        tvGetCode.setClickable(true); //设置不可点击
                        tvGetCode.setTextColor(getResources().getColor(R.color.common_color_42CC8B));
                    } else {
                        tvGetCode.setTextColor(getResources().getColor(R.color.common_color_d2d4dd));
                    }
                }
                checkBtnEnable();
            }
        });
        etCode.setOnTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onTextChange(String s) {
                checkBtnEnable();
            }
        });

    }

    private void checkBtnEnable() {
        String number = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        if (EmptyUtils.isEmpty(number)) {
            btnNext.setEnabled(false);
            return;
        }

        if (EmptyUtils.isEmpty(code)) {
            btnNext.setEnabled(false);
            return;
        }

        btnNext.setEnabled(true);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_get_code) {
            String phone = etPhone.getText().toString().trim();
            if (phone.length() == 11) {
                getCode();
            }
        } else if (id == R.id.btn_next) {
            String number = etPhone.getText().toString().trim();
            String code = etCode.getText().toString().trim();
            Bundle bundle = new Bundle();
            bundle.putString("number", number);
            bundle.putString("code", code);
            startActivity(ChangePasswordActivity.class, bundle, true);
        }
    }

    private void getCode() {
        String phone = etPhone.getText().toString().trim();
        HashMap<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        httpPost(UrlConstant.GET_CODE_URL, 0, params, false, "");
    }

    @Override
    protected void onSucceed(String result, int what) {
        super.onSucceed(result, what);
        try {
            if (what == 0) {
                JSONObject object = new JSONObject(result);
                String code = object.optString("code");
                String msg = object.optString("msg");
                if ("0".equals(code)) {
                    mCountDownTimer.start();
                } else {
                    toast(msg);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        mCountDownTimer.cancel();
    }
}
